package com.tjf.myjetpackdemo.observerModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: tjf
 * @date: 2023-06-07
 * @desc： 具体被观察者
 */
public class ConcreateObservable implements Observable {
    List<Observer> observerList;
    Object msg;

    public ConcreateObservable() {
        this.observerList = new ArrayList<>();
    }

    @Override
    public void add(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void remove(Observer observer) {
        observerList.remove(observer);
    }

    public void pushMsg(Object msg) {
        this.msg = msg;
        notifyObservers();
    }

    @Override
    public void notifyObservers() {
        for (Observer obs : observerList) {
            obs.update(msg);
        }
    }
}
